package model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashSet;
import java.util.Set;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlTransient;

import model.uml.note.Note;
import model.util.Point;

@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractStatechartComponent implements
		StatechartComponent {

	protected Long id;
	protected Long parentId;
	protected Point location;

	@XmlTransient
	protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);

	@XmlTransient
	private Set<Note> notes = new HashSet<Note>();

	public AbstractStatechartComponent(Long id, Long parentId, Point location) {
		this.id = id;
		this.parentId = parentId;
		this.location = location;
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public Long getParentId() {
		return parentId;
	}

	@Override
	public void setParentId(Long parentId) {
		Long oldParentId = this.parentId;
		this.parentId = parentId;
		propertyChangeSupport.firePropertyChange(Property.PARENT.name(),
				oldParentId, parentId);
	}

	@Override
	public Set<Note> getNotes() {
		return notes;
	}

	@Override
	public void setNotes(Set<Note> notes) {
		Set<Note> oldNotes = this.notes;
		this.notes = notes;
		propertyChangeSupport.firePropertyChange(Property.NOTES.name(),
				oldNotes, notes);
	}

	@Override
	public Point getLocation() {
		return location;
	}

	@Override
	public void setLocation(Point location) {
		Point oldLocation = this.location;
		this.location = location;
		propertyChangeSupport.firePropertyChange(Property.LOCATION.name(),
				oldLocation, location);
	}

	public void addPropertyChangeListener(Property property,
			PropertyChangeListener listener) {
		this.propertyChangeSupport.addPropertyChangeListener(property.name(),
				listener);
	}

	public void removePropertyChangeListener(Property property,
			PropertyChangeListener listener) {
		this.propertyChangeSupport.removePropertyChangeListener(
				property.name(), listener);
	}

}
